#!/bin/sh

# pithosctl - remote control helper script for Pithos (via DBus interface)

# Copyright 2014 Phillip Warner <pc_warner@yahoo.com>, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE

# Thanks goes out to Kevin Mehall for creating Pithos and providing the DBus
# interface (/usr/lib/python2.7/site-packages/pithos/dbus_service.py)
# Kevin's helpful post regarding this interface:
#      https://answers.launchpad.net/pithos/+question/166326

# Note, the DBus interface works regardless if media keys are enabled.
# In particular, if you would normally get the message
# "ERROR:root:Could not bind media keys" you can edit your 
# ~/.config/pithos.ini file and set "enable_mediakeys" to " False" to 
# prevent that error and still use the DBus interface successfully.

VERSION=0.1

# Control Functions
NEXT="SkipSong"
PLAYPAUSE="PlayPause"
LOVE="LoveCurrentSong"
BAN="BanCurrentSong"
TIRED="TiredCurrentSong"

set -e

usage() {
	echo "$(basename $0) $VERSION - by Phillip Warner"
        echo "Usage:"
        echo "  $0 [OPTION]"
	echo "Only one parameter can be used at a time."
        echo "The script's parameters are:"
        echo "  -h, --help		Help"
	echo "  -n, --next		Play Next"
	echo "  -p, --pause		Toggle Play / Pause"
	echo "  -l, --love		Love Song"
	echo "  -b, --ban		Ban Song"
	echo "  -t, --tired		Tired of Song"
	echo "  -q, --quit		Quit Program (Send Pithos HUP)"
	echo
	echo "Current pithos PIDs (euid=$(id -u)):"
	pgrep -u $(id -u) pithos$
}

# Make sure pithos is running and that there is no more than one arg
if ! (pgrep -u $(id -u) pithos$ &> /dev/null) || [ $2 ]
then
	usage
elif [ $1 ]
then
   case $1 in
      -h|--help ) usage ; exit
	;;
      -n|--next ) command=$NEXT
	;;
      -p|--pause ) command=$PLAYPAUSE
	;;
      -l|--love ) command=$LOVE
	;;
      -b|--ban ) command=$BAN
	;;
      -t|--tired ) command=$TIRED
	;;
      -q|--quit ) pkill -HUP -u $(id -u) pithos$ ; exit
	;;
      * ) usage ; exit
        ;;
   esac
   
   # access DBus interface using $command
   dbus-send --print-reply --dest=net.kevinmehall.Pithos /net/kevinmehall/Pithos net.kevinmehall.Pithos.$command &> /dev/null
   
else
	usage
fi

exit
